<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>订单管理 - 物流信息管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        .navbar {
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            z-index: 1030;
            height: 60px;
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }
        .sidebar {
            position: fixed;
            top: 60px;
            bottom: 0;
            left: 0;
            z-index: 1020;
            width: 240px;
            background-color: #fff;
            box-shadow: 1px 0 4px rgba(0,0,0,.1);
            overflow-y: auto;
        }
        .sidebar-sticky {
            padding-top: 1rem;
        }
        .main-content {
            margin-left: 240px;
            padding: 80px 20px 20px;
            min-height: calc(100vh - 60px);
        }
        .nav-link {
            color: #333;
            padding: 10px 20px;
            display: block;
            border-radius: 4px;
            margin: 4px 8px;
        }
        .nav-link:hover {
            background-color: #f8f9fa;
            text-decoration: none;
        }
        .nav-link.active {
            background-color: #0d6efd;
            color: white;
        }
        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        .card {
            margin-bottom: 20px;
            border: none;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            border-radius: 8px;
        }
        .table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }
        .btn-icon {
            padding: 0.25rem 0.5rem;
            margin: 0 0.25rem;
        }
        .search-box {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">物流信息管理系统</a>
            <div class="d-flex align-items-center">
                <span class="navbar-text me-3" id="userInfo">
                    欢迎，<span id="username"></span>
                </span>
                <button class="btn btn-outline-danger btn-sm" onclick="logout()">退出登录</button>
            </div>
        </div>
    </nav>

    <!-- 侧边栏 -->
    <nav class="sidebar">
        <div class="sidebar-sticky">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link" href="/dashboard">
                        <i class="fas fa-tachometer-alt"></i>
                        仪表盘
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="/order">
                        <i class="fas fa-box"></i>
                        订单管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/transport">
                        <i class="fas fa-truck"></i>
                        运输管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/warehouse">
                        <i class="fas fa-warehouse"></i>
                        仓库管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/storage">
                        <i class="fas fa-database"></i>
                        存储记录
                    </a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <main class="main-content">
        <div class="container-fluid">
            <!-- 搜索框 -->
            <div class="search-box">
                <div class="row g-3">
                    <div class="col-md-3">
                        <input type="text" class="form-control" id="orderNo" placeholder="订单号">
                    </div>
                    <div class="col-md-3">
                        <select class="form-select" id="status">
                            <option value="">全部状态</option>
                            <option value="0">待处理</option>
                            <option value="1">处理中</option>
                            <option value="2">已完成</option>
                            <option value="3">已取消</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <input type="date" class="form-control" id="startDate">
                    </div>
                    <div class="col-md-3">
                        <input type="date" class="form-control" id="endDate">
                    </div>
                    <div class="col-12">
                        <button class="btn btn-primary" onclick="searchOrders()">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                        <button class="btn btn-success" onclick="showCreateModal()">
                            <i class="fas fa-plus"></i> 新建订单
                        </button>
                    </div>
                </div>
            </div>

            <!-- 订单列表 -->
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>订单号</th>
                                    <th>发件人</th>
                                    <th>发件人电话</th>
                                    <th>收件人</th>
                                    <th>收件人电话</th>
                                    <th>收件地址</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="orderList"></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <!-- 创建订单模态框 -->
    <div class="modal fade" id="createModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">创建订单</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="createForm">
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">发货人姓名</label>
                                <input type="text" class="form-control" name="senderName" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">发货人电话</label>
                                <input type="tel" class="form-control" name="senderPhone" required>
                            </div>
                            <div class="col-md-12">
                                <label class="form-label">发货地址</label>
                                <input type="text" class="form-control" name="senderAddress" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">收货人姓名</label>
                                <input type="text" class="form-control" name="receiverName" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">收货人电话</label>
                                <input type="tel" class="form-control" name="receiverPhone" required>
                            </div>
                            <div class="col-md-12">
                                <label class="form-label">收货地址</label>
                                <input type="text" class="form-control" name="receiverAddress" required>
                            </div>
                            <div class="col-md-12">
                                <label class="form-label">备注</label>
                                <textarea class="form-control" name="remark" rows="3"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="createOrder()">创建</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 查看订单模态框 -->
    <div class="modal fade" id="viewModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">订单详情</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body" id="orderDetails"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑订单模态框 -->
    <div class="modal fade" id="editModal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑订单</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="editForm">
                        <input type="hidden" name="id">
                        <div class="row g-3">
                            <div class="col-md-6">
                                <label class="form-label">发件人姓名</label>
                                <input type="text" class="form-control" name="senderName" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">发件人电话</label>
                                <input type="text" class="form-control" name="senderPhone" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">收件人姓名</label>
                                <input type="text" class="form-control" name="receiverName" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">收件人电话</label>
                                <input type="text" class="form-control" name="receiverPhone" required>
                            </div>
                            <div class="col-md-12">
                                <label class="form-label">收货地址</label>
                                <input type="text" class="form-control" name="receiverAddress" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">货物类型</label>
                                <input type="text" class="form-control" name="goodsType" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">货物重量(kg)</label>
                                <input type="number" step="0.01" class="form-control" name="goodsWeight" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">货物体积(m³)</label>
                                <input type="number" step="0.01" class="form-control" name="goodsVolume" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">状态</label>
                                <select class="form-select" name="status" required>
                                    <option value="">请选择状态</option>
                                    <option value="0">待处理</option>
                                    <option value="1">已接单</option>
                                    <option value="2">运输中</option>
                                    <option value="3">已送达</option>
                                    <option value="4">已完成</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="updateOrder()">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
    <script>
        $(function() {
            // 获取用户信息
            $.get('/api/user/info', function(res) {
                if (res.code === 200) {
                    $('#username').text(res.data.username);
                } else {
                    window.location.href = '/login';
                }
            });

            // 加载订单列表
            loadOrders();
        });

        function getStatusText(status) {
            const statusMap = {
                0: '待处理',
                1: '已接单',
                2: '运输中',
                3: '已送达',
                4: '已完成'
            };
            return statusMap[status] || '未知状态';
        }

        function loadOrders() {
            $.get('/api/logistics-order/list', function(res) {
                if (res.code === 200) {
                    let html = '';
                    res.data.forEach(function(order) {
                        html += `
                            <tr>
                                <td>${order.orderNo || ''}</td>
                                <td>${order.senderName || ''}</td>
                                <td>${order.senderPhone || ''}</td>
                                <td>${order.receiverName || ''}</td>
                                <td>${order.receiverPhone || ''}</td>
                                <td>${order.receiverAddress || ''}</td>
                                <td>${getStatusText(order.status)}</td>
                                <td>${order.createTime || ''}</td>
                                <td>
                                    <button class="btn btn-sm btn-info" onclick="viewOrder(${order.id})">
                                        <i class="fas fa-eye"></i>
                                    </button>
                                    <button class="btn btn-sm btn-primary" onclick="editOrder(${order.id})">
                                        <i class="fas fa-edit"></i>
                                    </button>
                                    <button class="btn btn-sm btn-danger" onclick="deleteOrder(${order.id})">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </td>
                            </tr>
                        `;
                    });
                    $('#orderList').html(html);
                }
            });
        }

        function showCreateModal() {
            const modal = new bootstrap.Modal(document.getElementById('createModal'));
            modal.show();
        }

        function createOrder() {
            const formData = {};
            $('#createForm').serializeArray().forEach(item => {
                if (item.value) {
                    formData[item.name] = item.value;
                }
            });

            // 生成订单号
            formData.orderNo = generateOrderNo();

            $.ajax({
                url: '/api/logistics-order',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res) {
                    if (res.code === 200) {
                        alert('创建成功');
                        const modal = bootstrap.Modal.getInstance(document.getElementById('createModal'));
                        modal.hide();
                        loadOrders();
                    } else {
                        alert(res.message || '创建失败');
                    }
                },
                error: function(xhr) {
                    alert('系统错误：' + xhr.responseText);
                }
            });
        }

        function generateOrderNo() {
            const date = new Date();
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
            return `${year}${month}${day}${random}`;
        }

        function viewOrder(orderNo) {
            $.get('/api/logistics-order/' + orderNo, function(res) {
                if (res.code === 200) {
                    const order = res.data;
                    let html = `
                        <div class="row">
                            <div class="col-md-6">
                                <p><strong>订单编号：</strong>${order.orderNo}</p>
                                <p><strong>发件人：</strong>${order.senderName}</p>
                                <p><strong>发件人电话：</strong>${order.senderPhone}</p>
                                <p><strong>发件地址：</strong>${order.senderAddress}</p>
                            </div>
                            <div class="col-md-6">
                                <p><strong>收件人：</strong>${order.receiverName}</p>
                                <p><strong>收件人电话：</strong>${order.receiverPhone}</p>
                                <p><strong>收件地址：</strong>${order.receiverAddress}</p>
                                <p><strong>状态：</strong>${getStatusText(order.status)}</p>
                            </div>
                            <div class="col-12">
                                <p><strong>货物类型：</strong>${order.goodsType}</p>
                                <p><strong>货物重量：</strong>${order.goodsWeight}kg</p>
                                <p><strong>货物体积：</strong>${order.goodsVolume}m³</p>
                                <p><strong>创建时间：</strong>${order.createTime}</p>
                            </div>
                        </div>
                    `;
                    $('#orderDetails').html(html);
                    $('#viewModal').modal('show');
                }
            });
        }

        function editOrder(id) {
            $.get(`/api/logistics-order/${id}`, function(res) {
                if (res.code === 200) {
                    const order = res.data;
                    $('#editForm input[name="id"]').val(order.id);
                    $('#editForm input[name="senderName"]').val(order.senderName || '');
                    $('#editForm input[name="senderPhone"]').val(order.senderPhone || '');
                    $('#editForm input[name="receiverName"]').val(order.receiverName || '');
                    $('#editForm input[name="receiverPhone"]').val(order.receiverPhone || '');
                    $('#editForm input[name="receiverAddress"]').val(order.receiverAddress || '');
                    $('#editForm input[name="goodsType"]').val(order.goodsType || '');
                    $('#editForm input[name="goodsWeight"]').val(order.goodsWeight || '');
                    $('#editForm input[name="goodsVolume"]').val(order.goodsVolume || '');
                    $('#editForm select[name="status"]').val(order.status || '');
                    const modal = new bootstrap.Modal(document.getElementById('editModal'));
                    modal.show();
                }
            });
        }

        function updateOrder() {
            const formData = {};
            $('#editForm').serializeArray().forEach(item => {
                if (item.value) {
                    formData[item.name] = item.value;
                }
            });

            $.ajax({
                url: '/api/logistics-order',
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res) {
                    if (res.code === 200) {
                        alert('更新成功');
                        const modal = bootstrap.Modal.getInstance(document.getElementById('editModal'));
                        modal.hide();
                        loadOrders();
                    } else {
                        alert(res.message || '更新失败');
                    }
                },
                error: function(xhr) {
                    alert('系统错误：' + xhr.responseText);
                }
            });
        }

        function deleteOrder(orderNo) {
            if (!confirm('确定要删除这个订单吗？')) return;

            $.ajax({
                url: '/api/logistics-order/' + orderNo,
                type: 'DELETE',
                success: function(res) {
                    if (res.code === 200) {
                        loadOrders();
                    } else {
                        alert(res.msg);
                    }
                }
            });
        }

        function searchOrders() {
            const params = {
                orderNo: $('#orderNo').val() || null,
                status: $('#status').val() || null,
                startDate: $('#startDate').val() || null,
                endDate: $('#endDate').val() || null
            };

            $.ajax({
                url: '/api/logistics-order/search',
                type: 'GET',
                data: params,
                success: function(res) {
                    if (res.code === 200) {
                        let html = '';
                        res.data.forEach(function(order) {
                            html += `
                                <tr>
                                    <td>${order.orderNo || ''}</td>
                                    <td>${order.senderName || ''}</td>
                                    <td>${order.senderPhone || ''}</td>
                                    <td>${order.receiverName || ''}</td>
                                    <td>${order.receiverPhone || ''}</td>
                                    <td>${order.receiverAddress || ''}</td>
                                    <td>${getStatusText(order.status)}</td>
                                    <td>${order.createTime || ''}</td>
                                    <td>
                                        <button class="btn btn-sm btn-info" onclick="viewOrder(${order.id})">
                                            <i class="fas fa-eye"></i>
                                        </button>
                                        <button class="btn btn-sm btn-primary" onclick="editOrder(${order.id})">
                                            <i class="fas fa-edit"></i>
                                        </button>
                                        <button class="btn btn-sm btn-danger" onclick="deleteOrder(${order.id})">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </td>
                                </tr>
                            `;
                        });
                        $('#orderList').html(html);
                    } else {
                        alert(res.message || '搜索失败');
                    }
                },
                error: function(xhr) {
                    alert('系统错误：' + xhr.responseText);
                }
            });
        }

        function logout() {
            $.post('/api/user/logout', function() {
                window.location.href = '/login';
            });
        }
    </script>
</body>
</html> 